//
//  BChooseCoinTypeController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/5.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BChooseCoinTypeController.h"
//cell
#import "BCUserWalletCurrCell.h"
//refresh
#import "BCRefreshNormalHeader.h"

//request
#import "BCUserWalletCurrRequest.h"

@interface BChooseCoinTypeController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *typeTable;

/**
 *refreshHeader
 */
@property (nonatomic, strong) BCRefreshNormalHeader *refreshHeader;

@end

@implementation BChooseCoinTypeController

- (BCRefreshNormalHeader *)refreshHeader {
    if (!_refreshHeader) {
        _refreshHeader = [BCRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshHeaderAction)];
    }
    return _refreshHeader;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    [self setupSubviews];
    
    //
    if (self.walletCurrData) {
        [self reloadData:self.walletCurrData];
        [self refreshHeaderAction];
    }else {
        [self.refreshHeader beginRefreshing];
    }
}

- (void)setupSubviews {
    [self.customNaviBar.leftButton setImage:nil forState:UIControlStateNormal];
    [self.customNaviBar.rightButton setImage:LOAD_IMAGE(@"general_close") forState:UIControlStateNormal];
    self.customNaviBar.rightButton.imageEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 10);
    //
    self.typeTable = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.typeTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.typeTable.dataSource = self;
    self.typeTable.delegate = self;
    [self.view addSubview:self.typeTable];
    [self.typeTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.customNaviBar.mas_bottom);
        make.left.bottom.right.mas_equalTo(self.view);
    }];
    [self.typeTable registerClass:BCUserWalletCurrCell.class forCellReuseIdentifier:NSStringFromClass(BCUserWalletCurrCell.class)];
    //
    [self.typeTable setMj_header:self.refreshHeader];
    //
    [self.view layoutIfNeeded];
}
- (void)didClickedNaviRightButton {
    [self dismissViewControllerAnimated:true completion:nil];
}

#pragma mark - refreshHeaderAction
- (void)refreshHeaderAction {
    [self requestCoinTypeData];
}

#pragma mark -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.walletCurrData.data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BCUserWalletCurrCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(BCUserWalletCurrCell.class)];
    cell.currData = self.walletCurrData.data[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BCUserWalletCurrDataModel *model = self.walletCurrData.data[indexPath.row];
    if (self.delegate && [self.delegate respondsToSelector:@selector(chooseCoinTypeControllerDidSelectCurrDataModel:)]) {
        [self.delegate chooseCoinTypeControllerDidSelectCurrDataModel:model];
    }
    [self didClickedNaviRightButton];
}

#pragma mark - request
- (void)requestCoinTypeData {
    BCUserWalletCurrRequest *request = [BCUserWalletCurrRequest new];
    WEAK_SELF
    [request didRequestCompleted:^{
        [wself.refreshHeader endRefreshing];
    }];
    [request didRequestSuccessed:^(id result, id page) {
        BCUserWalletCurrResponse *resp = result;
        [wself reloadData:resp.data];
    }];
    [request didRequestFailed:^(NSError *error) {
        if (error&&wself.view) {
            NSString *errorMsg = error.domain;
            [QMUITips showError:errorMsg inView:wself.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:errorMsg]];
        }
    }];
    [request requestServer];
}
- (void)reloadData:(BCUserWalletCurrData *)currData {
    self.walletCurrData = currData;
    //
    self.title = currData.flow.curr_select;
    //
    [self.typeTable reloadData];
    //
    if (self.delegate&&[self.delegate respondsToSelector:@selector(chooseCoinTypeControllerDidRequestCurrDataSuccess:)]) {
        [self.delegate chooseCoinTypeControllerDidRequestCurrDataSuccess:currData];
    }
}


@end
